package com.weilele.leisure.time.ui.module.main

import android.os.Bundle
import android.view.View
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import com.weilele.base.library.BaseBindingFragment
import com.weilele.base.library.FragmentContainerActivity
import com.weilele.leisure.time.R
import com.weilele.leisure.time.bean.PageByIdBean
import com.weilele.leisure.time.databinding.FragmentHomePageBinding
import com.weilele.leisure.time.ui.module.history.HistoryVideoFragment
import com.weilele.leisure.time.ui.module.search.SearchActivity
import com.weilele.leisure.time.ui.module.splash.SplashViewModel
import com.weilele.leisure.time.ui.module.video_cache.VideoCacheFragment
import com.weilele.mvvm.base.livedata.appCompatActivity
import com.weilele.mvvm.base.helper.createViewModel
import com.weilele.mvvm.base.livedata.LiveDataWrap
import com.weilele.mvvm.utils.activity.toast
import com.weilele.mvvm.utils.view_page.simplePagerAdapter

class HomePagerFragment : BaseBindingFragment<FragmentHomePageBinding>() {
    companion object {
        fun open(appCompatActivity: AppCompatActivity) {
            FragmentContainerActivity.start<HomePagerFragment>(appCompatActivity)
        }
    }

    private val splashViewModel by lazy { createViewModel<SplashViewModel>() }

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
    }

    override fun initData() {
        super.initData()
        //加载数据
        splashViewModel.loginAndGetPageData()
    }

    override fun getObserverLiveData(): List<LiveDataWrap>? {
        return listOf(
            //监听请求用户信息
            splashViewModel.pageByIdData.wrapSwitchView {
                initPage(it.data)
            }
        )
    }

    override fun isNeedSwitchView(): Boolean {
        return true
    }

    override fun getClickView(): List<View?>? {
        return listOf(mBinding.ivHistory, mBinding.ivSearch, mBinding.ivDownload)
    }

    override fun onSingleClick(view: View) {
        super.onSingleClick(view)
        when (view) {
            mBinding.ivHistory -> {
                HistoryVideoFragment.start(appCompatActivity!!)
            }
            mBinding.ivDownload -> {
                VideoCacheFragment.start(appCompatActivity!!)
            }
            mBinding.ivSearch -> {
                SearchActivity.start(appCompatActivity!!)
            }
        }
    }

    private fun initPage(pageData: PageByIdBean?) {
        pageData?.components?.firstOrNull()?.childComponents?.let { datas ->
            val count = datas.count()
            mBinding.viewPage.simplePagerAdapter(this, count) {
                val data = datas[it]
                MainFragment.newInstance(data)
            }
            mBinding.tabLayout.attachViewPager2(mBinding.viewPage) { tab, position ->
                tab.text = datas[position].title
            }
        }
    }

    private var lastClickTime = 0L
    override fun onBackPressed(): OnBackPressedCallback? {
        if (appCompatActivity !is FragmentContainerActivity) {
            return null
        }
        return object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                val currentTime = System.currentTimeMillis()
                if (currentTime - lastClickTime <= 2000L) {
                    appCompatActivity?.finishAfterTransition()
                } else {
                    lastClickTime = currentTime
                    R.string.click_again_to_back_tips.toast()
                }
            }
        }
    }
}